// https://leetcode.cn/problems/maximum-average-subarray-i/

class Solution {
public:
    double findMaxAverage(vector<int>& nums, int k) {
        int ans = INT_MIN;
        int sum = 0;
        for (int i = 0; i < nums.size(); ++i) {
            sum += nums[i];
            if (i >= k) sum -= nums[i - k];
            if (i >= k - 1) ans = max(ans, sum);
        }
        return (double)ans / k;
    }
};

class Solution {
public:
    double findMaxAverage(vector<int>& nums, int k) {
        int sum = 0;
        for (int i = 0; i < k; ++i) {
            sum += nums[i]; // 先加上k个
        }
        int ans = sum;
        for (int i = k; i < nums.size(); ++i) {
            sum = sum + nums[i] - nums[i - k];
            ans = max(ans, sum);
        }
        return (double)ans / k;
    }
};